package com.example.pageradapterdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.pageradapterdemo.fragment.Demo1Fragment;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerDemo2Activity extends AppCompatActivity {
    private ViewPager viewPager;
    private List<String> dataList;
    private FPagerAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager_demo1);
        viewPager = findViewById(R.id.content_vp);
        initData();
        viewPager.setAdapter(mAdapter = new FPagerAdapter(getSupportFragmentManager(),dataList));

        final Button btnUpdate = findViewById(R.id.btnUpdate);
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.set(0,"VUE");
                mAdapter.updateData(dataList);
            }
        });
        final Button btnAdd = findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.add("Flutter");
                mAdapter.updateData(dataList);
            }
        });

        final Button btnRemove = findViewById(R.id.btnRemove);
        btnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.remove(0);
                mAdapter.updateData(dataList);
            }
        });

        final Button btnClear = findViewById(R.id.btnClear);
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataList.clear();
                mAdapter.updateData(dataList);
            }
        });
    }
    private void initData(){
        dataList = new ArrayList<>();
        dataList.add("Java");
        dataList.add("Android");
        dataList.add("C&C++");
        dataList.add("OC");
        dataList.add("Swift");
    }

    public class FPagerAdapter extends FragmentPagerAdapter{
        private List<Fragment> fragments;
        private FragmentManager fragmentManager;
        public FPagerAdapter(@NonNull FragmentManager fm,List<String> types) {
            super(fm);
            this.fragmentManager = fm;
            fragments = new ArrayList<>();
            for (String type : types) {
                fragments.add(Demo1Fragment.newInstance(type));
            }
            setFragments(fragments);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }

        private void updateData(List<String> types){
            List<Fragment> fragmentList = new ArrayList<>();
            for (String type : types) {
                Log.e("type",type);
                fragmentList.add(Demo1Fragment.newInstance(type));
            }
            setFragments(fragmentList);
        }

        @Override
        public int getCount() {
            return this.fragments.size();
        }

        private void setFragments(List<Fragment> fragments){
            if (fragments != null) {
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                for (Fragment fragment : fragments) {
                    transaction.remove(fragment);
                }
                transaction.commit();
                fragmentManager.executePendingTransactions();
            }
            this.fragments = fragments;
            notifyDataSetChanged();
        }

        @Override
        public int getItemPosition(@NonNull Object object) {
            return POSITION_NONE;
        }
    }
}